package cn.zyq.ykb.controller.mgt.task;

import cn.zyq.ykb.beans.task.MyTaskAudit;
import cn.zyq.ykb.beans.task.Task;
import cn.zyq.ykb.beans.task.bo.AddTaskPackageBo;
import cn.zyq.ykb.beans.task.bo.MgtTaskAddBo;
import cn.zyq.ykb.beans.task.bo.TaskAuditBo;
import cn.zyq.ykb.beans.task.bo.TaskTopBo;
import cn.zyq.ykb.beans.task.dto.MyTaskAuditDto;
import cn.zyq.ykb.beans.task.dto.ScreenDto;
import cn.zyq.ykb.service.task.MyTaskAuditService;
import cn.zyq.ykb.service.task.MyTaskService;
import cn.zyq.ykb.service.task.TaskService;
import cn.zyq.ykb.system.annotation.Log;
import cn.zyq.ykb.system.common.BaseController;
import cn.zyq.ykb.system.common.PageParam;
import cn.zyq.ykb.system.ry.enums.BusinessType;
import cn.zyq.ykb.system.ry.util.poi.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.math3.ml.neuralnet.twod.util.TopographicErrorHistogram;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 任务Controller
 *
 * @author SystemAuth
 * @date 2023-05-22
 */

@RestController(value = "MgtTaskController")
@RequestMapping("/mgt/task")
@Api(tags = "任务接口")
@Slf4j
public class TaskController extends BaseController {
    @Autowired
    private TaskService taskService;
    @Autowired
    private MyTaskService myTaskService;
    @Autowired
    private MyTaskAuditService myTaskAuditService;

    @ApiOperation(value = "领取任务审批")
    @PostMapping(value = "/audit")
    public void audit(@RequestBody TaskAuditBo param) {
        this.taskService.audit(param, this.getLoginSysAdmin());
    }

    @ApiOperation(value = "发布任务审批")
    @PostMapping(value = "/auditTask")
    public void auditTask(@RequestBody TaskAuditBo param) {
        synchronized (param.getServiceID().intern()) {
            this.taskService.auditTask(param, this.getLoginSysAdmin());
        }
    }

    /**
     * 查询任务列表
     */
    @ApiOperation(value = "管理端列表", response = Task.class)
    @PostMapping("/list")
    public Object list(@RequestBody PageParam pageParam) {
        return this.taskService.selectAllList(pageParam, this.getLoginSysAdmin());
    }

    /**
     * 导出任务列表
     */
    @ApiOperation(value = "导出")
    @Log(title = "任务" , businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@RequestBody PageParam pageParam) {
        List<Task> list = taskService.daoChu(pageParam);
        ExcelUtil<Task> util = new ExcelUtil<Task>(Task. class);
        util.exportExcel(response, list, "任务数据");
    }

    /**
     * 获取任务详细信息
     */
    @ApiOperation(value = "详情", response = Task.class)
    @GetMapping(value = "/{taskID}")
    public Object getInfo(@PathVariable("taskID") Integer taskID) {
        return this.taskService.selectTaskByTaskIDMgt(taskID, this.getLoginUser());
    }
    /**
     * 获取任务详细信息
     */
    @ApiOperation(value = "详情", response = Task.class)
    @GetMapping(value = "/detail/{serviceID}")
    public Object getInfo(@PathVariable(value = "serviceID") String serviceID) {
        return this.taskService.selectTaskByServiceID(serviceID);
    }

    /**
     * 新增任务
     */
    @ApiOperation(value = "新增")
    @Log(title = "任务", businessType = BusinessType.INSERT)
    @PostMapping
    public Object add(@RequestBody MgtTaskAddBo bo) {
        return this.taskService.addTask(bo,this.getLoginSysAdmin());
    }

    /**
     * 修改任务
     */
    @ApiOperation(value = "编辑")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public Object edit(@RequestBody MgtTaskAddBo bo) {
        return this.taskService.uptTask(bo,this.getLoginSysAdmin());
    }

    /**
     * 删除任务
     */
    @ApiOperation(value = "删除")
    @Log(title = "任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{taskIDs}")
    public Object remove(@PathVariable Integer[] taskIDs) {
        return this.taskService.deleteTaskByTaskIDs(taskIDs);
    }

    /**
     * 获取任务详细信息
     */
    @ApiOperation(value = "根据任务ID查询领取信息", response = Task.class)
    @PreAuthorize("@ss.hasPermi('ykb:task:query')")
    @PostMapping(value = "/getMyTask")
    public Object getMyTask(@RequestBody PageParam pageParam) {
        return this.taskService.getMyTask(pageParam);
    }

    /**
     * 根据任务ID查询平台任务审核信息
     */
    @ApiOperation(value = "根据任务ID查询平台任务审核信息", response = Task.class)
    @PostMapping(value = "/getMyTaskAudit")
    public Object getMyTaskAudit(@RequestBody PageParam pageParam) {
        return this.taskService.getMyTaskAudit(pageParam,this.getLoginSysAdmin());
    }

    /**
     * 根据任务ID查询悬赏任务审核信息
     */
    @ApiOperation(value = "根据任务ID查询悬赏任务审核信息", response = Task.class)
    @PostMapping(value = "/getMyTaskAuditXS")
    public Object getMyTaskAuditXS(@RequestBody PageParam pageParam) {
        return this.taskService.getMyTaskAuditXS(pageParam,this.getLoginSysAdmin());
    }

    /**
     * 获取任务详细信息
     */
    @ApiOperation(value = "添加进任务包", response = Task.class)
    @PreAuthorize("@ss.hasPermi('ykb:task:query')")
    @PostMapping(value = "/addTaskPackage")
    public Object addTaskPackage(@RequestBody AddTaskPackageBo bo) {
        return this.taskService.addTaskPackage(bo,this.getLoginSysAdmin());
    }


    /**
     * 发布任务数
     */
    @ApiOperation(value = "发布任务数", response = Task.class)
//    @PreAuthorize("@ss.hasPermi('ykb:task:query')")
    @GetMapping(value = "/getTaskNumber")
    public Object getTaskNumber() {
        return this.taskService.getTaskNumber();
    }

    /**
     * 接取订单数
     */
    @ApiOperation(value = "接取订单数", response = Task.class)
//    @PreAuthorize("@ss.hasPermi('ykb:task:query')")
    @GetMapping(value = "/getMyTaskNumber")
    public Object getMyTaskNumber() {
        return this.myTaskService.getMyTaskNumber(this.getLoginSysAdmin());
    }

    /**
     * 任务类型统计
     */
    @ApiOperation(value = "任务类型统计", response = Task.class)
//    @PreAuthorize("@ss.hasPermi('ykb:task:query')")
    @GetMapping(value = "/getTaskTypeNumber")
    public Object getTaskTypeNumber() {
        return this.taskService.getTaskTypeNumber(this.getLoginSysAdmin());
    }

    /**
     * 一周订单统计
     */
    @ApiOperation(value = "一周订单统计")
    @GetMapping("/getRegisterNumber/{time}")
    public Object getRegisterNumber(@PathVariable("time") String time) {
        return this.taskService.getRegisterNumber(time);
    }


    /**
     * 导出任务列表
     */
    @ApiOperation(value = "导出")
//    @PreAuthorize("@ss.hasPermi('ykb:myTaskAudit:export')")
//    @Log(title = "任务-审核对象" , businessType = BusinessType.EXPORT)
    @PostMapping("/daoChu")
    public void daoChu(@RequestBody PageParam pageParam) {
        log.info("传递参数，{}",pageParam);
        List<MyTaskAuditDto> list = myTaskAuditService.daoChu(pageParam,this.getLoginSysAdmin());
        log.info("导出对象，{}",list);
        ExcelUtil<MyTaskAuditDto> util = new ExcelUtil<MyTaskAuditDto>(MyTaskAuditDto. class);
        util.exportExcel(response, list, "任务审核数据");
    }

    /**
     * 校验任务名是否重复
     */
    @ApiOperation(value = "校验任务名是否重复")
    @GetMapping("/check/{productName}")
    public Object check(@PathVariable("productName") String productName) {
        return this.taskService.check(productName);
    }

    /**
     * 屏蔽任务
     */
    @ApiOperation(value = "屏蔽任务")
    @PostMapping("/screen")
    public Object screen(@RequestBody ScreenDto dto) {
        return this.taskService.screen(dto.getTaskIDs(), this.getLoginSysAdmin());
    }

    /**
     * 恢复任务
     */
    @ApiOperation(value = "恢复任务")
    @PostMapping("/restore")
    public Object restore(@RequestBody ScreenDto dto) {
        return this.taskService.restore(dto.getTaskIDs(),this.getLoginSysAdmin());
    }
    /**
     * 批量置顶
     */
    @ApiOperation(value = "批量置顶")
    @PostMapping("/batchTop")
    public Object batchTop(@RequestBody TaskTopBo bo) {
        return this.taskService.batchTop(bo,this.getLoginSysAdmin());
    }

    /**
     * 批量垫底
     */
    @ApiOperation(value = "批量垫底")
    @PostMapping("/batchFall")
    public Object batchFall(@RequestBody TaskTopBo bo) {
        return this.taskService.batchFall(bo,this.getLoginSysAdmin());
    }
}
